package jbrowser.gui;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.URI;
import javax.swing.DefaultListModel;
import javax.swing.JList;

public class HistoryPage extends AbstractPage {

    public HistoryPage() {
        initComponents();
        lstHistory.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent evt) {
                JList list = (JList) evt.getSource();
                if (evt.getClickCount() == 2) {
                    if (list.getSelectedIndex() == list.locationToIndex(evt.getPoint())) {
                        URI uri = (URI) list.getSelectedValue();
                        int jumpIndex = tab.getHistory().indexOf(uri);
                        tab.getHistory().jump(jumpIndex);
                        tab.newPage(uri);
                    }
                }
            }
        });
    }

    @Override
    public void setTab(Tab tab) {
        this.tab = tab;
        DefaultListModel model = new DefaultListModel();
        for (URI uri : tab.getHistory().getHistory()) {
            model.add(0, uri);
        }
        lstHistory.setModel(model);
    }

    @Override
    public String getTitle() {
        return "History";
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblMessage = new javax.swing.JLabel();
        scrHistory = new javax.swing.JScrollPane();
        lstHistory = new javax.swing.JList();

        setLayout(new java.awt.BorderLayout(5, 5));

        lblMessage.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        lblMessage.setText("History");
        lblMessage.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        add(lblMessage, java.awt.BorderLayout.NORTH);

        scrHistory.setViewportView(lstHistory);

        add(scrHistory, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel lblMessage;
    private javax.swing.JList lstHistory;
    private javax.swing.JScrollPane scrHistory;
    // End of variables declaration//GEN-END:variables
}
